package com.dms.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.dms.service.*;
import com.dms.model.*;

import java.util.*;
@Controller
@RequestMapping("admin")
public class AdminLogAction {
	@Resource
	private StuLogService slService;
	@Resource
	private BuildingService bService;
	@Resource
	private StudentService sService;
	@Resource
	private DomitoryService dService;
	@Resource
	private TeacherService tService;
	
	
	@RequestMapping("logManager")
	public ModelAndView logManager(){
		ModelAndView mav=new ModelAndView();
		List<StuLog> stuLogList=slService.getStuLogList();
		mav.addObject("stuLogList", stuLogList);
		mav.setViewName("logManager");
		return mav;
	}
	
	@RequestMapping("preSaveLog")
	public ModelAndView preSaveLog(HttpServletRequest req){
		ModelAndView mav=new ModelAndView();

		List<Building> buildingList=bService.getBuildingList();
		mav.addObject("buildingList", buildingList);
		
		String bId=req.getParameter("bId");
		if(bId!=null){
			int bid=Integer.parseInt(bId);
			
			Building currentBuilding=bService.getBuilding(bid);
			List<Student> inStudentList=sService.getStudentsByBuilding(bid);
			List<Teacher> teacherList=tService.getTeachersByBuilding(bid);
			
			mav.addObject("inStudentList", inStudentList);
			mav.addObject("currentBuilding", currentBuilding);
			mav.addObject("teacherList", teacherList);
		}
		
		mav.setViewName("addLog");
		return mav;
	}
	
	@RequestMapping("saveLog")
	public String saveLog(HttpServletRequest req){
		String sId=req.getParameter("sId");
		String tId=req.getParameter("tId");
		String logDate=req.getParameter("logDate");
		String remark=req.getParameter("remark");
		Student student=sService.getStudent(Integer.parseInt(sId));
		Teacher teacher=tService.getTeacher(Integer.parseInt(tId));
		
		StuLog stuLog=new StuLog(teacher, student, logDate, remark);
		slService.saveStuLog(stuLog);
		System.out.println("path:"+req.getContextPath());
		
		return "redirect:/admin/logManager.do";
	}
	
	@RequestMapping("deleteLog")
	public String deleteLog(@RequestParam("id") int id){
		slService.deleteStuLog(id);
		return "redirect:/admin/logManager.do";
	}
}
